﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    [WaiGaoQiaoWeb.FilterSession.AuthorizeAttribute]
    public class OutstockSporadicController : Controller
    {
        //
        // GET: /OutstockSporadic/

        public ActionResult OutstockSporadicIndex()
        {
            return View();
        }
        /// <summary>
        /// 获取食堂
        /// </summary>
        /// <returns></returns>
        public SelectList GetYaxNode()
        {
            YaxNodeInfo yninfo = new YaxNodeInfo(true);
            IList<YaxNodeInfo> ynlist = yninfo.SelYaxNodeInfo("and CONFLAG=1");
            ynlist.ToList();
            return new SelectList(ynlist, "NodeID", "Nodename");
        }
        private OutstockSporadicInfo outstocksporadic = new OutstockSporadicInfo(true);
        /// <summary>
        /// 主页面获取记录
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetOutstockSporadicList(PageVM pvm)
        {
            if (string.IsNullOrEmpty(pvm.query))
            {
                pvm.query = "and OrderNo = ''";
            }
            string condition = pvm.query;
            JsonFlexiGridData jsonFlexiGridData = SetJsonFlexiGridData(condition, pvm.rp, pvm.page);
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 增加页面获取记录
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult AddGetOutstockSporadicList(PageVM pvm)
        {
            string condition = "and OrderNo = '" + pvm.query + "'";
            JsonFlexiGridData jsonFlexiGridData = SetJsonFlexiGridData(condition, pvm.rp, pvm.page);
            return Json(jsonFlexiGridData);
        }

        public JsonFlexiGridData SetJsonFlexiGridData(string condition, int pagesize, int startindex)
        {
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = startindex;
            jsonFlexiGridData.total = outstocksporadic.GetOutstockGroupSundriesCount(condition);

            IList<OutstockSporadicInfo> list = outstocksporadic.GetOutstockGroupInfo(pagesize, startindex, condition);

            List<string> v;
            foreach (OutstockSporadicInfo ida in list)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.LeanDt.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo));
                v.Add(ida.GoodsID.ToString());
                v.Add(Con_GoodsInfo.GetGoodsNameByID(ida.GoodsID));
                v.Add(YaxNodeInfo.GetYaxNodeName(ida.LeanDept));
                v.Add(ida.LeanCount.ToString());
                v.Add(ida.Operator_ID.ToString());
                v.Add(ida.RecordDateTime.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo));
                v.Add(ida.OrderNo);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return jsonFlexiGridData;
        }
        //*******************************************************
        //  新建零星物品出库
        //*******************************************************
        public ActionResult AddOutstockSporadic(int id, string orderno)
        {
            ViewData["canteen"] = GetYaxNode();
            OutstockGroupSporadicVM ouspvm = new OutstockGroupSporadicVM();
            if (id == 0)
            {
                ouspvm.ID = 0;
                ouspvm.LeanDt = DateTime.Now.Date;
                ouspvm.OrderNo = outstocksporadic.BuliderOrderNo();
                ouspvm.RecordDateTime = DateTime.Now.Date;
                ouspvm.Operator_ID = ((LogOnModel)System.Web.HttpContext.Current.Session["user"]).LogName;
            }
            else
            {
                OutstockSporadicInfo outspinfo = outstocksporadic.GetGetOutstockGroupInfo(id);
                Mapper.DynamicMap(outspinfo, ouspvm);
            }
            return View(ouspvm);
        }
        /// <summary>
        /// 保存零星物品出库
        /// </summary>
        /// <param name="ouspvm"></param>
        /// <returns></returns>
        public ActionResult SaveOutstockSporadic(OutstockGroupSporadicVM ouspvm)
        {
            int rowcount = 0;
            OutstockSporadicInfo outspinfo = Mapper.DynamicMap<OutstockSporadicInfo>(ouspvm);
            if (ModelState.IsValid)
            {
                if (ouspvm.ID == 0)
                {
                    rowcount = outstocksporadic.SaveOutstockSundriesGroup(outspinfo);
                }
                else
                {
                    rowcount = outstocksporadic.UpdateInstockSundriesGroup(outspinfo);
                }
            }
            return Json(rowcount);
        }
        /// <summary>
        /// 删除零星物品出库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DeleteSporadic(int id)
        {
            int rowcount = 0;
            rowcount = outstocksporadic.DeleteOutstockGroup(id);
            return Json(rowcount);
        }
        /// <summary>
        /// 出库验证
        /// </summary>
        /// <returns></returns>
        public ActionResult OutStockYzh(int Con_GoodsInfoId, decimal LeanCount)
        {
            bool istongguo = outstocksporadic.OutStockYzh(Con_GoodsInfoId, LeanCount);
            return Json(istongguo);
        }

        /// <summary>
        /// 出库验证
        /// </summary>
        /// <returns></returns>
        public ActionResult OutStockYzh1(string sel)
        {
            string[] strSel = sel.Split(',');
            decimal result = -1;
            try
            {
                result = Convert.ToDecimal(strSel[1]);
                bool istongguo = outstocksporadic.OutStockYzh(Convert.ToInt32(strSel[0]), Convert.ToDecimal(strSel[1]));
                if (istongguo == false)
                {
                    result = -2;
                }
            }
            catch
            {
                result = -3;
            }
            return Json(result);
        }
    }
}
